package com.android.accessibility.stat;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.android.accessibility.NickInfo;
import com.android.accessibility.provider.DBConstant;
import com.android.accessibility.provider.DBHelper;
import com.android.accessibility.utils.Log;
import com.android.accessibility.utils.Utils;

public class StatManager {

    private Context mContext;
    private static StatManager sStatManager = null;
    private DBHelper mDBHelper;

    private StatManager(Context context) {
        mContext = context;
        mDBHelper = new DBHelper(mContext);
    }

    public static StatManager get(Context context) {
        if (sStatManager == null) {
            sStatManager = new StatManager(context);
        }
        return sStatManager;
    }

    public void insertData(CharSequence nickName, long time, int type) {
        insertData(nickName, time, type, 0);
    }

    public void insertData(CharSequence nickName, long time, int type, int funs) {
        SQLiteDatabase writable = mDBHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBConstant.NICK_NAME, String.valueOf(nickName));
        values.put(DBConstant.NICK_NAME_HASH,
                Utils.string2MD5(String.valueOf(nickName)));
        values.put(DBConstant.NICK_TYPE, type);
        values.put(DBConstant.NICK_FUNS, funs);
        values.put(DBConstant.OP_TIME, time);
        String label = "未知";
        if (type == DBConstant.TYPE_LIST) {
            label = "昵称列表";
        } else if (type == DBConstant.TYPE_SENT) {
            label = "已发送";
        }
        values.put(DBConstant.NICK_TYPELABEL, label);
        if (type == DBConstant.TYPE_LIST) {
            label = "默认";
            if (funs == DBConstant.FUNS_BLACK) {
                label = "黑名单";
            } else if (funs == DBConstant.FUNS_WHITE) {
                label = "白名单";
            }
            values.put(DBConstant.NICK_FUNSLABEL, label);
        }
        writable.replace(DBConstant.TABLE_NICKLIST, DBConstant.FOO, values);
        writable.close();
    }

    public List<NickInfo> getBlackList() {
        return getNickList(DBConstant.FUNS_BLACK);
    }

    public List<NickInfo> getWhiteList() {
        return getNickList(DBConstant.FUNS_WHITE);
    }

    public List<NickInfo> getNickList(int funs) {
        SQLiteDatabase readable = mDBHelper.getReadableDatabase();
        Cursor c = null;
        String sql = "select * from " + DBConstant.TABLE_NICKLIST + " where " + DBConstant.NICK_FUNS + "=" + funs;
        List<NickInfo> list = null;
        try {
            c = readable.rawQuery(sql, null);
            if (c != null && c.moveToFirst()) {
                list = new ArrayList<NickInfo>();
                NickInfo info = null;
                do {
                    info = new NickInfo();
                    info.setId(c.getInt(c.getColumnIndex(DBConstant._ID)));
                    info.setNickName(c.getString(c.getColumnIndex(DBConstant.NICK_NAME)));
                    info.setNickType(c.getInt(c.getColumnIndex(DBConstant.NICK_TYPE)));
                    info.setOpTime(c.getLong(c.getColumnIndex(DBConstant.OP_TIME)));
                    list.add(info);
                } while (c.moveToNext());
            }
        } catch (Exception e) {
            Log.d(Log.TAG, "error : " + e);
        } finally {
            if (c != null) {
                c.close();
            }
        }
        readable.close();
        return list;
    }

    public boolean isBlack(CharSequence nickName) {
        return isExist(nickName, DBConstant.TYPE_LIST, DBConstant.FUNS_BLACK);
    }

    public boolean isWhite(CharSequence nickName) {
        /*
        String md5 = Utils.string2MD5(String.valueOf(nickName));
        if ("402051f4be0cc3aad33bcf3ac3d6532b".equals(md5)
                || "52ef0af336c65cdc8c221e79246b7171".equals(md5)
                || "ab1a1ba9a8a389711d8440750a02cfa5".equals(md5)) {
            return true;
        }*/
        return isExist(nickName, DBConstant.TYPE_LIST, DBConstant.FUNS_WHITE);
    }

    public boolean isSent(CharSequence nickName) {
        return isExist(nickName, DBConstant.TYPE_SENT, 0);
    }

    private boolean isExist(CharSequence nickName, int type, int funs) {
        String md5 = Utils.string2MD5(String.valueOf(nickName));
        String sql = null;
        String [] selectionArgs = null;
        if (type == DBConstant.TYPE_SENT) {
            sql = "select * from " + DBConstant.TABLE_NICKLIST + " where "
                    + DBConstant.NICK_NAME_HASH + "='" + md5 + "' AND "
                    + DBConstant.NICK_TYPE + "='" + type + "'";
        } else if (type == DBConstant.TYPE_LIST){
            sql = "select * from " + DBConstant.TABLE_NICKLIST + " where "
                    + DBConstant.NICK_NAME_HASH + "='" + md5 + "' AND "
                    + DBConstant.NICK_TYPE + "='" + type + "' AND "
                    + DBConstant.NICK_FUNS + "='" + funs + "'";
        }
        SQLiteDatabase readable = mDBHelper.getReadableDatabase();
        boolean isExist = false;
        try {
            Cursor c = readable.rawQuery(sql, selectionArgs);
            if (c != null && c.getCount() > 0) {
                isExist = true;
            }
            c.close();
            readable.close();
        } catch (Exception e) {
            Log.d(Log.TAG, "error : " + e);
        }
        return isExist;
    }

    public int getSentCount() {
        String sql = "select count(*) as sentcount from "
                + DBConstant.TABLE_NICKLIST + " where " + DBConstant.NICK_TYPE
                + "=" + DBConstant.TYPE_SENT;
        SQLiteDatabase readable = mDBHelper.getReadableDatabase();
        int count = 0;
        try {
            Cursor c = readable.rawQuery(sql, null);
            if (c != null && c.moveToFirst()) {
                count = c.getInt(c.getColumnIndex("sentcount"));
            }
            c.close();
            readable.close();
        } catch (Exception e) {
            Log.d(Log.TAG, "error : " + e);
        }
        return count;
    }

    public void clearSent() {
        String sql = "delete from " + DBConstant.TABLE_NICKLIST + " where "
                + DBConstant.NICK_TYPE + "=" + DBConstant.TYPE_SENT;
        SQLiteDatabase writable = mDBHelper.getWritableDatabase();
        writable.execSQL(sql);
        writable.close();
    }

    public void deleteByIds(List<Integer> list) {
        if (list == null || list.size() <= 0) {
            return;
        }
        StringBuilder builder = new StringBuilder();
        for (Integer integer : list) {
            builder.append(integer);
            builder.append(",");
        }
        builder = builder.deleteCharAt(builder.length() - 1);
        String area = builder.toString();
        String sql = "delete from " + DBConstant.TABLE_NICKLIST + " where _id in (" + area + ")"; 
        SQLiteDatabase writable = mDBHelper.getWritableDatabase();
        writable.execSQL(sql);
        writable.close();
    }
}
